{% include 'admin/header.html' %}
<script type="text/javascript">
    changeTab('作业管理');
    var $in;
    var $score_td = null;

    $(function () {
        var $load = $('#loading');
        $(document).ajaxStart(function () {
            $load.removeClass('hidden');
        });
        $(document).ajaxStop(function () {
            $load.addClass('hidden');
        });

        $('#years').change(function () {  // 改变选择框
            var year = $(this).val().substring(0, 4);
            $.post('/class/by_year', {year: year}, function (data) {
                var $clazz = $('#clazz');
                $clazz.html('');
                $.each(data.result, function (k, v) {
                    var ht = '<option data-kid="' + v.id + '">' + v.k_name + '</option>';
                    $clazz.append(ht);
                });
            });
            $.post('/by_year', {year: year}, function (data) {
                var $stu = $('#stu');
                $stu.html('');
                $.each(data.result, function (k, v) {
                    var ht = '<option data-sid="' + v.id + '">' + v.stu_name + '</option>';
                    $stu.append(ht);
                });
            });
        });

        $('#search-wk').click(function () {  // 搜索操作
            var year = $('#years').val().substring(0, 4);
            var kid = $('#clazz option:selected').data('kid');
            var sid = $('#stu option:selected').data('sid');
            var status = $('#status').val();
            var d = {year: year, kid: kid, sid: sid};
            $.each(d, function (k, v) {
                if (v === 0) {
                    delete d[k]
                }
            });
            d['status'] = status;

            console.log(d);
            $.post('/work/query', d, function (data) {
                fillData(data);
            });
        });

        $('.do_mark form').submit(function (e) {
            e.preventDefault();
            var $in = $(this).find('input');
            var wid = $in.data('wid');
            var score = $in.val().trim();
            $.post('/work/do_mark/'+wid, {score: score}, function (data) {
                if (data.status) {
                    alert('SUCCESS: ' + data.msg);
                    $score_td.html(score);
                } else {
                    alert('ERROR: ' + data.msg);
                }
                close_();
                $score_td = null;
            });
        });

        // 分页
        $('.pagination').on('click', 'li', function (e) {
            e.preventDefault();
            var page = $(this).data('page');
            var year = $('#years').val().substring(0, 4);
            var kid = $('#clazz option:selected').data('kid');
            var sid = $('#stu option:selected').data('sid');
            var d = {page: page, year: year, kid: kid, sid: sid};
            $.each(d, function (k, v) {
                if (v === 0) {
                    delete d[k]
                }
            });
            d['status'] = $('#status').val();

            var status = $(this).attr("class");
            console.log(d);

            if (page > 0 && status !== "active") {
                {#console.log(d);#}
                $.post('/work/query', d, function (data) {
                    fillData(data);
                });
            }
        });


        $in = $('input[type="file"]');

        $in.change(function () {  // 上传文件
        var file = $in[0].files[0];
            fileUp(kid, sid, file, function (data) {
                if (data.status) {
                    alert('SUCCESS: ' + data.msg);
                    btn.parents('tr').find('span[class^=work-]').attr('class', 'work-2');
                    btn.parent().prepend('<a href="' + data.work_url + '" class="btn btn-xs btn-warning">下载</a>');
                    btn.remove();
                } else {
                    alert('UPWORK ERROR: ' + data.msg)
                }
            });
        });
    });

    // 打分
    function MARK(self) {
        var $mark_box = $('.do_mark');
        var wid = $(self).data('id');
        var $tds = $(self).parent().siblings();
        $score_td = $tds.eq(5);
        $mark_box.find('div input').data('wid', wid).val('');
        $mark_box.find('div p:first-child span').html($tds.eq(1).html());
        $mark_box.find('div p:last-child span').html($tds.eq(2).html());
        $mark_box.removeClass('hidden');
    }

    var sid = 0;
    var kid = 0;
    var btn = null;
    // 上传文件操作
    function UPWORK(self) {
        sid = $(self).data('sid');
        kid = $(self).data('kid');
        btn = $(self);
        $in.click();
    }

    function select_(page) {
        var lis = $('.pagination').find('li');
        $.each(lis, function (k, v) {
            var val = $(v).find('a').text();
            if (!/\W/.test(val)) {
                if (val != page) { // 非严格比较 允许字符串和数字相等
                    $(v).removeClass('active');
                } else {
                    $(v).addClass('active');
                }
            }
        });
    }

    function fillData(data) {
        var $tbody = $('tbody');
        $tbody.html('');

        var $p_box = $('.pagination');
        $p_box.html('');
        $('#page-div').addClass('hidden');
        if (data.status) {
            var i = 1;
            $.each(data.result.result, function (k, v) {
                var dq = '';
                if (v.work_status == 0){
                    dq = '<button onclick="UPWORK(this);" data-kid="'+v.k.id+'" data-sid="'+v.stu.id+'"\n' +
                                'class="up_work btn btn-xs btn-success">补交</button>';
                } else {
                    dq = '<a href="' + v.work_url + '" class="btn btn-xs btn-warning">下载</a>';
                }
                var h = '<tr>\n' +
                    ' <td>' + i++ + '</td>\n' +
                    ' <td>' + v.k.k_name + '</td>\n' +
                    ' <td>' + v.stu.stu_name + '</td>\n' +
                    ' <td><span class="work-' + v.work_status + '"></span></td>\n' +
                    ' <td>' + v.work_uptime + '</td>\n' +
                    ' <td>' + (v.work_score < 0 ? '未打分' : v.work_score) + '</td>\n' +
                    ' <td>\n' + dq +
                    '   <button onclick="MARK(this);" data-wid=' + v.id + ' class="btn btn-xs btn-info">打分</button>\n' +
                    ' </td>\n' +
                    '</tr>';
                $tbody.append(h);
            });

            if (data.result.pages > 1) {
                $('#page-div').removeClass('hidden');
                $p_box.append('<li data-page="1"><a href="javascript:">首页</a></li>');
                $.each(data.result.pageNums, function (i, j) {
                    $p_box.append('<li data-page="' + j + '"><a href="javascript:">' + j + '</a></li>');
                });
                $p_box.append('<li data-page="' + data.result.pages + '"><a href="javascript:">尾页</a></li>');
                $('#page-div > span').html('共' + data.result.pages + '页，' + data.result.total + '条数据');
            }
        } else {
            $tbody.html('<tr><td align="center" colspan="7">无该记录</td></tr>');
        }

        select_(data.result.page);
    }

    function close_() {
        $('.do_mark').addClass('hidden');
    }

    function W_Export_() {
        var year = $('#years').val().substring(0, 4);
        var kid = $('#clazz option:selected').data('kid');
        var sid = $('#stu option:selected').data('sid');
        var status = $('#status').val();
        window.open('/work/export?year=' + year +
            '&kid=' + kid + '&sid=' + sid + '&status=' + status, '_self');
    }
</script>
<div class="work-xcontent">
    <div class="do_mark hidden" style="position: absolute; width: 100vw; height: 100vh; top: 0; left:0; z-index: 1000; opacity: 1;">
        <div style="position: absolute; top: 25vh; left: 48vw;">
            <div style="width: 240px;" class="thumbnail">
                <a href="javascript:" onclick="close_();" class="pull-right">&times;</a>
                <div style="margin: 5px 5px -5px;">
                    <p>课程：<span></span></p>
                    <p>学生：<span></span></p>
                </div>
                <form>
                <div style="margin: 0 8px;">
                    <input onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
                           class="form-control" placeholder="分数" maxlength="4">
                </div>
                <div class="caption text-center">
                    <button type="button" onclick="close_();"
                            class="btn btn-danger btn-sm">取消</button>
                    <button type="submit" class="btn btn-info btn-sm">打分</button>
                </div>
                </form>
            </div>
        </div>
    </div>
    <div>
        <div class="col-lg-12 col-xs-12">
            <h4><span class="fa fa-hand-o-right"></span> 作业管理</h4>
            <div style="padding: 15px 5px 0;" class="pull-right">
                <div id="loading" class="pull-left hidden" style="padding: 4px 10px;">
                    <img class="" width="20" src="/static/images/loading.jpg" alt="加载中">
                </div>
                <div style="margin-right: 4px;" class="pull-left">
                    <button onclick="W_Export_()" class="btn btn-sm btn-success">导出</button>
                </div>
                <div class="input-group min-min-input pull-left">
                    <select id="status" class="form-control min-min-select">
                        <option value="-1">全部</option>
                        <option value="0">未交</option>
                        <option value="1">已交</option>
                        <option value="2">逾期提交</option>
                    </select>
                </div>
                <div class="input-group min-min-input pull-left">
                    <select id="years" class="form-control min-min-select">
                        {% for foo in years %}
                            {% if loop.index > 1 %}
                                <option {% if years[0] == foo.k_time %}selected{% endif %}>
                                    {{ foo.k_time }}年度
                                </option>
                            {% endif %}
                        {% endfor %}
                    </select>
                </div>
                <div class="input-group min-min-input pull-left">
                    <select id="clazz" style="width: 140px;" class="form-control min-min-select">
                        {% for foo in clzs %}
                            <option data-kid="{{ foo.id }}">{{ foo.k_name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="input-group min-min-input pull-left">
                    <select id="stu" class="form-control min-min-select">
                        {% for foo in stus %}
                            <option data-sid="{{ foo.id }}">{{ foo.stu_name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <button id="search-wk" class="btn btn-info btn-sm">搜索</button>
            </div>
        </div>
        <div>
            <div class="col-lg-12 col-xs-12">
                <input class="hidden" type="file">
                <table class="table table-striped">
                    <thead class="bg-color-x">
                    <tr>
                        <td>索引</td>
                        <td>课程名</td>
                        <td>学生名</td>
                        <td>提交状态</td>
                        <td>提交时间</td>
                        <td>分数</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody>
                    {% if not work_list or work_list.total <= 0 %}
                        <tr>
                            <td align="center" colspan="7"> 暂无数据</td>
                        </tr>
                    {% else %}
                        {% for a in work_list.result %}
                            <tr>
                                <td>{{ loop.index }}</td>
                                <td>{{ a.k.k_name }}</td>
                                <td>{{ a.stu.stu_name }}</td>
                                <td><span class="work-{{ a.work_status }}"></span></td>
                                <td>{{ a.work_uptime }}</td>
                                <td>{% if a.work_score < 0 %}未打分{% else %}{{ a.work_score }}{% endif %}</td>
                                <td>
                                    {% if a.work_status == 0 %}
                                        <button onclick="UPWORK(this);" data-kid="{{ a.k.id }}" data-sid="{{ a.stu.id }}"
                                                class="up_work btn btn-xs btn-success">补交</button>
                                    {% else %}
                                        <a href="{{ a.work_url }}" class="btn btn-xs btn-warning">下载</a>
                                    {% endif %}
                                    <button onclick="MARK(this);" data-id="{{ a.id }}" class="btn btn-xs btn-info">打分</button>
                                </td>
                            </tr>
                        {% endfor %}
                    {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <!-- 分页按钮 -->
    <div id="page-div"
         class="col-lg-12 col-xs-12 text-center {% if work_list.pages <= 1 %}hidden{% endif %}">
        <ul style="margin: 0;" class="pagination pagination-sm">
            <li data-page="1"><a href="javascript:">首页</a></li>
            {% for foo in work_list.pageNums %}
                <li data-page="{{ foo }}" class="{% if foo == work_list.page %}active{% endif %}">
                    <a href="javascript:">{{ foo }}</a>
                </li>
            {% endfor %}
            <li data-page="{{ work_list.pages }}"><a href="javascript:">尾页</a></li>
        </ul>
        <span style="padding-top: 15px; font-size: 12px;" class="pull-right">
            共{{ work_list.pages }}页，{{ work_list.total }}条数据
        </span>
    </div>
</div>
<script src="/static/style/js/work_up.js"></script>
{% include 'admin/footer.html' %}